package com.aihna.mall.controller;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.aihna.common.core.domain.model.LoginUser;
import com.aihna.common.satoken.utils.LoginHelper;
import com.aihna.common.tenant.helper.TenantHelper;
import com.aihna.mall.domain.bo.UserOperateIntegralMoneyBo;
import com.aihna.mall.domain.bo.UserUpdateSpreadBo;
import com.aihna.mall.domain.vo.StoreUserInfoVo;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.aihna.common.idempotent.annotation.RepeatSubmit;
import com.aihna.common.log.annotation.Log;
import com.aihna.common.web.core.BaseController;
import com.aihna.common.mybatis.core.page.PageQuery;
import com.aihna.common.core.domain.R;
import com.aihna.common.core.validate.AddGroup;
import com.aihna.common.core.validate.EditGroup;
import com.aihna.common.log.enums.BusinessType;
import com.aihna.common.excel.utils.ExcelUtil;
import com.aihna.mall.domain.vo.StoreUserVo;
import com.aihna.mall.domain.bo.StoreUserBo;
import com.aihna.mall.service.IStoreUserService;
import com.aihna.common.mybatis.core.page.TableDataInfo;

/**
 * 会员用户
 *
 * @author Joseph.Chen
 * @date 2024-04-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/storeUser")
public class StoreUserController extends BaseController {

    private final IStoreUserService storeUserService;

    /**
     * 查询会员用户列表
     */
    @SaCheckPermission("mall:storeUser:list")
    @GetMapping("/list")
    public TableDataInfo<StoreUserVo> list(StoreUserBo bo, PageQuery pageQuery) {
        return storeUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出会员用户列表
     */
    @SaCheckPermission("mall:storeUser:export")
    @Log(title = "会员用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StoreUserBo bo, HttpServletResponse response) {
        List<StoreUserVo> list = storeUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "会员用户", StoreUserVo.class, response);
    }

    /**
     * 获取会员用户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("mall:storeUser:query")
    @GetMapping("/{id}")
    public R<StoreUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(storeUserService.queryById(id));
    }

    /**
     * 新增会员用户
     */
    @SaCheckPermission("mall:storeUser:add")
    @Log(title = "会员用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StoreUserBo bo) {
        bo.setUserType("routine");
        return toAjax(storeUserService.insertByBo(bo));
    }

    /**
     * 修改会员用户
     */
    @SaCheckPermission("mall:storeUser:edit")
    @Log(title = "会员用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StoreUserBo bo) {
        return toAjax(storeUserService.updateByBo(bo));
    }

    /**
     * 删除会员用户
     *
     * @param ids 主键串
     */
    @SaCheckPermission("mall:storeUser:remove")
    @Log(title = "会员用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(storeUserService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 修改用户手机号
     * @param id 用户uid
     * @param phone 手机号
     */
    @SaCheckPermission("mall:storeUser:edit")
    @Log(title = "修改用户手机号", businessType = BusinessType.DELETE)
    @PostMapping(value = "/update/phone")
    public R<Void> updatePhone(@RequestParam(name = "id") Integer id, @RequestParam(name = "phone") String phone) {
        return toAjax(storeUserService.updateUserPhone(id, phone));
    }

    /**
     * 操作积分余额
     */
    @SaCheckPermission("mall:storeUser:edit")
    @Log(title = "操作积分余额", businessType = BusinessType.DELETE)
    @PostMapping(value = "/founds")
    public R<Void> founds(@Validated(EditGroup.class) @RequestBody UserOperateIntegralMoneyBo bo) {
        return toAjax(storeUserService.updateIntegralMoney(bo));
    }

    /**
     * 操作积分余额
     */
    @SaCheckPermission("mall:storeUser:edit")
    @Log(title = "操作积分余额", businessType = BusinessType.DELETE)
    @PostMapping(value = "/extension")
    public R<Void> extension(@Validated(EditGroup.class) @RequestBody UserUpdateSpreadBo bo) {
        return toAjax(storeUserService.updateSpread(bo));
    }
}
